<?php
require("conn.php");
require_once './../utility_definition/errorcode.php';
require_once './../shared_feature/checkAuth.php';

/*function checkpass($userName)
{
	$conn=mysql_open();
	mysql_select_db("paradigmshift_dev", $conn);
	$us=$userName;
	//$hash_from_db = db_get_hash($hash_from_db);
	//$pw=auth_verifyPassword($userPassHash, $hash_from_db);
	$result=mysql_query("select paradigmshift_dev.account.* from  paradigmshift_dev.account
			where account.accName='".$us."'");
	$num=mysql_num_rows($result);
	if($num)
	{
		mysql_close($conn);
		return true;
	}
	mysql_close($conn);
	return false;
}*/
function shiftStatus($userName, $userPassHash,$empID)
{
    $err=new ErrorCode();
	//$hash_from_db = db_get_hash('millions');
	//$us='Chen';
	$emid="";
	if(checkAuthentication($userName,$userPassHash))
	{
		$conn=mysql_open();
		mysql_select_db("paradigmshift_dev", $conn);
		 $count  =  count($empID); 
		if($count>0)
		{
		    $emid=" where";
			for($i=0;$i<$count;$i++)
			{
			   if($i<$count-1)
			   {
		       		$emid=$emid." empID=".$empID[$i]." or";
			   }
			   else
			   {
			        $emid=$emid." empID=".$empID[$i];
			   } 		
			}
		}
		$result=mysql_query("select roles.roleName,dates.datestamp,
roledate.roleID,roledate.dateID,
roledate.startTime,roledate.endTime,
roledate.shiftStatus
from  paradigmshift_dev.roledate left join (roles,dates)
on roles.roleID=roledate.roleID and dates.dateID=roledate.dateID".$emid,$conn);
		/*echo "<table id='users' class='ui-widget ui-widget-content'>
		 <thead>
		<tr class='ui-widget-header '>
		<th>roleID</th>
		<th>empID</th>
		<th>dateID</th>
		<th>StartDate</th>
		<th>EndDate</th>
		<th>shiftStatus</th>
		<th>date</th>
		</tr>
		</thead>";*/
		$count=mysql_num_rows($result);
		if ($count<=0)
	    {
		     return null;
		     $error =$err->failRead;
		}
		while($row=mysql_fetch_array($result))
		{
		$array[] = array(
				'roleName'=>$row['roleName'],
				'roleID'=>$row['roleID'],
				'dateID'=>$row['dateID'],
				'roleName'=>$row['roleName'],
				'date'=>$row['datestamp'],
				'startTime'=>$row['startTime'],
				'startTime'=>$row['endTime'],
				'shiftStatus'=>$row['shiftStatus'],
		}
		mysql_close($conn);
		//return $result;
		return $array;
		$error=$err->successRead;
	}
	else
	{
		//echo 'unknow user';
		$error =$err->failRead;
	}
}
/*-----------------------------------------------------------------------------------*/
function checkRangeSchedulePerWorker($userName, $userPassHash, $startDate, $endDate,$empID)
{
 $err=new ErrorCode();
	//$hash_from_db = db_get_hash('millions');
	$us=$userName;
	//$us='Chen';
	$emid="";
	if(checkAuthentication($userName,$userPassHash))
	{
		$conn=mysql_open();
		mysql_select_db("paradigmshift_dev", $conn);

		$sDate=date('y/m/d',strtotime(trim($startDate)));
		$eDate=date('y/m/d',strtotime(trim($endDate)));
		//$sDate='2013-01-17';
		//$eDate='2013-08-25';
		$result=mysql_query("select paradigmshift_dev.account.accName,roles.roleName,
roles.roleID,roledate.dateID,
paradigmshift_dev.roledate.startTime,
paradigmshift_dev.roledate.endTime,
paradigmshift_dev.roledate.shiftStatus,
paradigmshift_dev.dates.datestamp from  
paradigmshift_dev.employees left join (dates,roledate,account,roles) on          
paradigmshift_dev.roledate.empID=employees.empID and
paradigmshift_dev.dates.dateID=paradigmshift_dev.roledate.dateID and
account.empID=employees.empID and roles.roleID=roledate.roleID 
where dates.datestamp between '".$sDate."' and '".$eDate."' and employees.empID="$empID,$conn);
		/*echo "<table id='users' class='ui-widget ui-widget-content'>
		 <thead>
		<tr class='ui-widget-header '>
		<th>roleID</th>
		<th>empID</th>
		<th>dateID</th>
		<th>StartDate</th>
		<th>EndDate</th>
		<th>shiftStatus</th>
		<th>date</th>
		</tr>
		</thead>";*/
		$count=mysql_num_rows($result);
		if ($count<=0)
	    {
		     return null;
		     $error =$err->failRead;
		}
		while($row=mysql_fetch_array($result))
		{
		$array[] = array(
				'accName'=>$row['accName'],
				'roleName'=>$row['roleName'],
				'roleID'=>$row['roleID'],
				'dateID'=>$row['dateID'],
				'startTime'=>$row['startTime'],
				'startTime'=>$row['endTime'],
				'shiftStatus'=>$row['shiftStatus'],
				'date'=>$row['datestamp']);
		}
		mysql_close($conn);
		//return $result;
		return $array;
		$error=$err->successRead;
	}
	else
	{
		//echo 'unknow user';
		$error =$err->failRead;
	}
}
/*-----------------------------------------------------------------------------------*/
function checkRangeSchedule($userName, $userPassHash, $startDate, $endDate)
{
    $err=new ErrorCode();
	//$hash_from_db = db_get_hash('millions');
	$us=$userName;
	//$us='Chen';
	$emid="";
	if(checkAuthentication($userName,$userPassHash))
	{
		$conn=mysql_open();
		mysql_select_db("paradigmshift_dev", $conn);

		$sDate=date('y/m/d',strtotime(trim($startDate)));
		$eDate=date('y/m/d',strtotime(trim($endDate)));
		//$sDate='2013-01-17';
		//$eDate='2013-08-25';
		$result=mysql_query("select paradigmshift_dev.account.accName,roles.roleName,
roledate.roleID,roledate.dateID,
paradigmshift_dev.roledate.startTime,
paradigmshift_dev.roledate.endTime,
paradigmshift_dev.roledate.shiftStatus,
paradigmshift_dev.dates.datestamp from  
paradigmshift_dev.employees left join (dates,roledate,account,roles) on          
paradigmshift_dev.roledate.empID=employees.empID and
paradigmshift_dev.dates.dateID=paradigmshift_dev.roledate.dateID and
account.empID=employees.empID and roles.roleID=roledate.roleID 
where dates.datestamp between '".$sDate."' and '".$eDate."'",$conn);
		/*echo "<table id='users' class='ui-widget ui-widget-content'>
		 <thead>
		<tr class='ui-widget-header '>
		<th>roleID</th>
		<th>empID</th>
		<th>dateID</th>
		<th>StartDate</th>
		<th>EndDate</th>
		<th>shiftStatus</th>
		<th>date</th>
		</tr>
		</thead>";*/
		$count=mysql_num_rows($result);
		if ($count<=0)
	    {
		     return null;
		     $error =$err->failRead;
		}
		while($row=mysql_fetch_array($result))
		{
		$array[] = array(
				'accName'=>$row['accName'],
				'roleName'=>$row['roleName'],
				'roleID'=>$row['roleID'],
				'dateID'=>$row['dateID'],
				'startTime'=>$row['startTime'],
				'startTime'=>$row['endTime'],
				'shiftStatus'=>$row['shiftStatus'],
				'date'=>$row['datestamp']);
		}
		mysql_close($conn);
		//return $result;
		return $array;
		$error=$err->successRead;
	}
	else
	{
		//echo 'unknow user';
		$error =$err->failRead;
	}
}
/*---------------------------------------------------------------------*/
function checkDaySchedule($userName, $userPassHash, $Date)
{
    $err=new ErrorCode();
	$us=$userName;
	//$us='Chen';
	if(checkAuthentication($userName,$userPassHash))
	{
		$conn=mysql_open();
		mysql_select_db("paradigmshift_dev", $conn);
		$iDate=date('y/m/d',strtotime(trim($Date)));
		//$iDate='2013-01-25';
		$result=mysql_query("select paradigmshift_dev.account.accName,roles.roleName,
paradigmshift_dev.roledate.roleID,
paradigmshift_dev.roledate.dateID,
paradigmshift_dev.roledate.startTime,
paradigmshift_dev.roledate.endTime,
paradigmshift_dev.roledate.shiftStatus,
paradigmshift_dev.dates.datestamp from  
paradigmshift_dev.employees left join (dates,roledate,account,roles) 
on paradigmshift_dev.roledate.empID=employees.empID and
paradigmshift_dev.account.empID=paradigmshift_dev.employees.empID and
paradigmshift_dev.dates.dateID=paradigmshift_dev.roledate.dateID 
and roles.roleID=roledate.roleID
where dates.datestamp = '".$iDate."'",$conn);
		/*echo "<table id='users' class='ui-widget ui-widget-content'>
		 <thead>
		<tr class='ui-widget-header '>
		<th>roleID</th>
		<th>empID</th>
		<th>dateID</th>
		<th>StartDate</th>
		<th>EndDate</th>
		<th>shiftStatus</th>
		<th>date</th>
		</tr>
		</thead>";*/
		$count=mysql_num_rows($result);
		if ($count<=0)
	    {
		     return null;
		     $error =$err->failRead;
		}
		while($row=mysql_fetch_array($result))
		{
			$array[] = array(
			'accName'=>$row['accName'],
			'roleName'=>$row['roleName'],
			'roleID'=>$row['roleID'],
			'dateID'=>$row['dateID'],
			'startTime'=>$row['startTime'],
			'endTime'=>$row['endTime'],
			'shiftStatus'=>$row['shiftStatus'],
			'date'=>$row['datestamp']);
		}
		mysql_close($conn);
		//return $result;
		return $array;
		$error=$err->successRead;

	}
	else
	{
		//echo 'unknow user';
		$error =$err->failRead;
	}
}
/*--------------------------------------------------------------------------------*/
function viewMySchedule($userName, $userPassHash)
{
    $err=new ErrorCode();
	$us=$userName;
	//$us='Chen';
	if(checkAuthentication($userName,$userPassHash))
	{
		$conn=mysql_open();
		mysql_select_db("paradigmshift_dev", $conn);
		$iName=$userName;
		//$iName="dChen";
		$result=mysql_query("select roles.roleName,
paradigmshift_dev.roledate.dateID,
paradigmshift_dev.roledate.roleID,
paradigmshift_dev.roledate.startTime,
paradigmshift_dev.roledate.endTime,
paradigmshift_dev.roledate.shiftStatus,
paradigmshift_dev.dates.datestamp 
from  paradigmshift_dev.employees
left join (dates,roledate,account,roles) on paradigmshift_dev.roledate.empID=employees.empID and
paradigmshift_dev.dates.dateID=paradigmshift_dev.roledate.dateID
and paradigmshift_dev.roledate.empID=paradigmshift_dev.account.empID
and roles.roleID=roledate.roleID
where account.accName='".$iName."'",$conn);
		/*echo "<table id='users' class='ui-widget ui-widget-content'>
			<thead>
		<tr class='ui-widget-header '>
		<th>roleID</th>
		<th>empID</th>
		<th>dateID</th>
		<th>StartDate</th>
		<th>EndDate</th>
		<th>shiftStatus</th>
		<th>date</th>
		</tr>
		</thead>";*/
		$count=mysql_num_rows($result);
		if ($count<=0)
	    {
		     return null;
		     $error =$err->failRead;
		}
		while($row=mysql_fetch_array($result))
		{
			$array[] = array(
			'roleName'=>$row['roleName'],
			'roleID'=>$row['roleID'],
			'dateID'=>$row['dateID'],
			'startTime'=>$row['startTime'],
			'endTime'=>$row['endTime'],
			'shiftStatus'=>$row['shiftStatus'],
			'date'=>$row['datestamp']);
		}
		mysql_close($conn);
		return $array;
		$error=$err->successRead;
		//return $result;
	}
	else
	{
		//echo 'unknow user';
		$error =$err->failRead;
	}
}
?>